﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%@ Import Namespace="AgriMarketWebProj.AgriMarketService" %>
<%@ Import Namespace="AgriMarketWebProj.Models" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>
<% 
    Auction auction = Model;
    UserAccount currentUserAccount = ViewBag.CurrentUserAccount;
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    if (auction.Type.TypeId == 1)
    {
        Response.Write("<div class='buyAuction' id='buyAuction" + auction.AuctionId.ToString() + "'>");
    }
    else
    { 
      Response.Write("<div class='buyAuction_Sell' id='buyAuction" + auction.AuctionId.ToString() + "'>");
    }
    Response.Write("<div id='ErrorPanelDiv"+auction.AuctionId.ToString()+"' ></div>");
    Response.Write("<div class='topic' id='topic" + auction.AuctionId.ToString() + "'>");
    Response.Write("<a href='/Auction/ViewAuctionDetails?auctionId=" + auction.AuctionId + "'><span class='fontTopic'>" + auction.Topic + "</span></a>");
    Response.Write("</div>");
    Response.Write("<div class='info_picture' style='height:200px; width:580px; margin:auto;'>");
    Response.Write("<div  class='div_picture'>");
    Response.Write("<img class='picture' id= 'picture" + auction.AuctionId.ToString() + "'  src='" + ServiceURL.ServicePath + auction.ProductImage + "' />");
    Response.Write("</div>");
    Response.Write("<div class='info' id='info" + auction.AuctionId.ToString() + "'>");
    Response.Write("<table class='table_data'  >");
    Response.Write("<tr style='height:auto;'>");
    Response.Write("<td style='width:120px;'>nguoi dang</td>");
    Response.Write("<td id='userName" + auction.AuctionId.ToString() + "' style='margin:1px;'>"+ auction.AuthorId.ToString() + "</label></td>");
    Response.Write("</tr>");
    Response.Write("<tr style='height:auto'>");
    Response.Write("<td>ngay bat dau</td>");
    Response.Write("<td id='startedDate" + auction.AuctionId.ToString() + "'  >" + auction.StartedDate.ToString() + "</td>");
    Response.Write("</tr>");
    Response.Write("<tr style='height:auto'>");
    Response.Write("<td>ngay ket thuc</td>");
    Response.Write("<td id='finishedDate" + auction.AuctionId.ToString() + "'>" + auction.FinishedDate.ToString() + "</td>");
    Response.Write("</tr>");
    Response.Write("<tr style='height:auto'>");
    Response.Write("<td>ten san pham</td>");
    Response.Write("<td id='productName" + auction.AuctionId.ToString() + "'>" + auction.Product.ProductName.ToString() + "</td>");
    Response.Write("</tr>");
    Response.Write("<tr style='height:auto'>");
    Response.Write("<td>gia khoi diem</td>");
    Response.Write("<td id='initialPrice" + auction.AuctionId.ToString() + "' >" + auction.InitialPrice.ToString() + "</td>");
    Response.Write("</tr>");
    var serializedAuction = serializer.Serialize(auction);
    Response.Write("</table>");
    Response.Write("<script>setAutoLoadThresholdPrice(" + auction.AuctionId + ", " + auction.Type.TypeId + ");</script>");
    //if (auction.StartedDate <= DateTime.Now)
    //{
        if (auction.Type.TypeId == 1)
        {
            Response.Write("<div  class='thresholdPriceInfoDiv'>Giá cao nhất hiện tại: <span id='thresholdPriceInfoDiv" + auction.AuctionId + "' class='thresholdPriceInfoDiv'>N/A</span></div>");
        }
        else if (auction.Type.TypeId == 2)
        {
            Response.Write("<div  class='thresholdPriceInfoDiv'>Giá thấp nhất hiện tại: <span id='thresholdPriceInfoDiv" + auction.AuctionId + "' class='thresholdPriceInfoDiv'>N/A</span></div>");
        }
    //}
    //Response.Write("<span id='thresholdPriceInfoDiv" + auction.AuctionId + "' class='thresholdPriceInfoDiv'></span>");
    Response.Write("</div><!--end info-->");
    Response.Write("</div>");
    Response.Write("<div class='buttonrow_class'>");
    if (currentUserAccount != null  && currentUserAccount.TypeOfUser.TypeId != AdminCode.AdminTypeCode /*&& auction.StartedDate.CompareTo(DateTime.Now)>0*/)
    {
        if (auction.StartedDate >= DateTime.Now && auction.AuthorId == currentUserAccount.UserName)
        {
            Response.Write("<input class='button_row_item' type='image' id='updateButton" + auction.AuctionId.ToString() + "' name='updateButton" + auction.AuctionId.ToString() + "' value='Update' onclick='updateAuction(" + serializedAuction + ");' src='../../Content/Images/folderplus32.png'/>");

            Response.Write("<input class='button_row_item' type='image' id='deleteButton" + auction.AuctionId.ToString() + "' name='deleteButton" + auction.AuctionId.ToString() + "' value='Delete' onclick='deleteAuction(" + auction.AuctionId + ");' src='../../Content/Images/recycle32.png'/>");
        %>
        <%= "<form style='float:left;' method='post' id='auctionImageForm" + auction.AuctionId + "' name='auctionImageForm' action='/Auction/UpLoadImage' enctype='multipart/form-data'>"%>
        <%
            Response.Write("<input type='hidden' id='updatedAuctionJson' name='updatedAuctionJson' value='" + serializedAuction + "' />");
            Response.Write("<label for='Image" + auction.AuctionId + "' id='Image' ><img src='../../Content/Images/user32.png' class='button_row_item'></label>");
            Response.Write("<input type='file' id='Image" + auction.AuctionId + "' name='Image' class='auctionImageForm_Image' />");
            Response.Write("<input type='image' class='button_row_item' value='submit' src='../../Content/Images/bag32.png'/>");
        %>
        </form>
        <%
        
            Response.Write("<script type='text/javascript'>(function () {$('#auctionImageForm" + auction.AuctionId + "').ajaxForm({beforeSend: function () {}, uploadProgress: function (event, position, total, percentComplete) { }, success: function () { },  complete: function (xhr) {var newImagePath=xhr.responseText; if (newImagePath =='1') {$('#ErrorPanelDiv" + auction.AuctionId + "').html('Loi');} else { $('#picture" + auction.AuctionId + "' ).attr('src',newImagePath)}  }  });   })(); </script>");
        }
        if (auction.StartedDate<DateTime.Now && auction.FinishedDate >= DateTime.Now)
        {
            Response.Write("<input class='button_row_item' type='image' id='bidButton" + auction.AuctionId.ToString() + "' name='bidButton" + auction.AuctionId.ToString() + "' value='Bid' onclick='showBidPopUp(" + auction.AuctionId + "," + auction.Type.TypeId + ");' src='../../Content/Images/risegraph32.png'/>");
        }
        
    }
    if (currentUserAccount != null)
    {
        Response.Write("<input type='image' id='addFollowedAuction" + auction.AuctionId + "' name='addFollowedAuction" + auction.AuctionId + "' data-original-title='Xem riêng' data-placement='right' rel='tooltip' class='button_row_item'   src='../../Content/Images/wand32.png' onclick='addFollowedAuction(&apos;" + currentUserAccount.UserName + "&apos;," + auction.AuctionId + ");' />");
    }
    Response.Write("<input type='image' id='show_detail" + auction.AuctionId + "' name='showDetail' data-original-title='Hiển thị chi tiết' data-placement='right' rel='tooltip' class='button_row_item' style='float:right;' onclick='slideToggleDetailDiv(" + auction.AuctionId + ");' src='../../Content/Images/linedpaperplus32.png'/>");
    Response.Write("</div>");
    //Response.Write("</div>");
    Response.Write("<div class='detail' id='detail" + auction.AuctionId.ToString() + "' >");
    //Response.Write("<div id='DetailData" + auction.AuctionId.ToString() + "' class='Detail_Data' >");
    Response.Write(auction.ProductDescription);
    //Response.Write("</div>");
    Response.Write("</div>");
    //Response.Write("<div>");
    List<Auctioneer> auctioneerList = ViewBag.AuctioneerList;

    Response.Write("<div id='AuctionDetail" + auction.AuctionId.ToString() + "' class='auction_detail' >");
    Response.Write("<div class='auction_detail_input_next' >");
    if (auctioneerList != null && auctioneerList.Count > 0)
    {
        Response.Write("<input type='button' id='nextAuctioneersButton" + auction.AuctionId + "' class='AuctionerBidList' name='nextAuctioneersButton" + auction.AuctionId + "' value='Next Auctioneers' onclick='loadNextAuctioneers(" + auction.AuctionId + ");' />");
        Response.Write("<input type='hidden' id='nextLastAuctioneerId" + auction.AuctionId + "' class='AuctionerBidList'  name='nextLastAuctioneerId" + auction.AuctionId + "' value='" + auctioneerList[0].AuctioneerId + "'  />");
    }
    else
    {
        Response.Write("<input type='button' id='nextAuctioneersButton" + auction.AuctionId + "' class='AuctionerBidList'  name='nextAuctioneersButton" + auction.AuctionId + "' value='Next Auctioneers' onclick='loadNextAuctioneers(" + auction.AuctionId + ");' />");
        Response.Write("<input type='hidden' id='nextLastAuctioneerId" + auction.AuctionId + "' class='AuctionerBidList'  name='nextLastAuctioneerId" + auction.AuctionId + "' value='-1'  />");
    }
    Response.Write("</div>");
    Response.Write("<div class='auctioneersPanel' id='auctioneersPanel"+auction.AuctionId+"'>");
    Html.RenderPartial("AuctioneersList", auctioneerList);
    //Response.Write("</div>");
    Response.Write("</div>");
    if (auctioneerList != null && auctioneerList.Count > 0)
    {
        Response.Write("<div class='auction_detail_input_previous' >");
        Response.Write("<input type='button' id='previousAuctioneersButton" + auction.AuctionId + "' class='AuctionerBidList'  name='previousAuctioneersButton" + auction.AuctionId + "' value='Previous Auctioneers' onclick='loadPreviousAuctioneers(" + auction.AuctionId + ");' />");
        Response.Write("<input type='hidden' id='previousLastAuctioneerId" + auction.AuctionId + "' class='AuctionerBidList'  name='previousLastAuctioneerId" + auction.AuctionId + "' value='" + auctioneerList[auctioneerList.Count - 1].AuctioneerId + "'  />");
        Response.Write("</div>");
    }
    Response.Write("</div>");
    if (auction.StartedDate.CompareTo(DateTime.Now) <= 0 && auction.FinishedDate.CompareTo(DateTime.Now) >= 0) 
    {
        Response.Write("<script>setAutoLoadNewAuctioneers(" + auction.AuctionId + ");</script>");
    }
    if (currentUserAccount != null && currentUserAccount.TypeOfUser.TypeId != AdminCode.AdminTypeCode)
    {
        Response.Write("<div class='bidPopup' id='bidPopup" + auction.AuctionId + "'>");
        Response.Write("Giá: <input type='number' id='bidPrice" + auction.AuctionId + "' placeholder='Giá' min='0'  />");
        Response.Write("<div id='bidPriceError" + auction.AuctionId + "' ></div><br/>");
        Response.Write("<div id='thresholdPriceDiv" + auction.AuctionId + "' ></div>");
        Response.Write("<input type='hidden' id='thresholdPrice" + auction.AuctionId + "' value='0' />");
        if (auction.Type.TypeId == 1)
        {
            Response.Write("<input type='button' id='submitBidButton" + auction.AuctionId + "' value='Bid' onclick='submitBidBuyAuction(" + auction.AuctionId + ",&apos;" + currentUserAccount.UserName + "&apos;);' />");
        }
        else if (auction.Type.TypeId == 2)
        {
            Response.Write("<input type='button' id='submitBidButton" + auction.AuctionId + "' value='Bid' onclick='submitBidSellAuction(" + auction.AuctionId + ",&apos;" + currentUserAccount.UserName + "&apos;);' />");
        }
        Response.Write("<input type='button' class='cancelBidButton' id='cancelBidButton" + auction.AuctionId + "' value='Cancel' onclick='hideBidPopup(" + auction.AuctionId + ");' />");
        Response.Write("</div>");
    }
    Response.Write("</div>");
%>
